package edu.xpu.compute.recommend.restful.user;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import edu.xpu.compute.recommend.common.model.PageModel;
import edu.xpu.compute.recommend.common.model.RequestListParam;
import edu.xpu.compute.recommend.common.model.ResponseParam;
import edu.xpu.compute.recommend.entity.user.UserCollect;
import edu.xpu.compute.recommend.service.user.UserCollectService;

@RequestMapping(value="restful/user/collect")
@RestController
public class UserCollectRestful {
	@Autowired
	private UserCollectService collectService;

	/**
	 * 获取分页列表信息
	 * @param requestListParam
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@PostMapping(value="list")
	public ResponseParam<PageModel<UserCollect>> list(@RequestBody(required=false) RequestListParam<UserCollect> requestListParam) {
		if (requestListParam == null) {
			requestListParam = new RequestListParam<>();
		}
		PageModel<UserCollect> pageByConditions = collectService.getPageByConditions(requestListParam.getNumber()
													,requestListParam.getSize()
													,requestListParam.getEntity()
													,requestListParam.getSortField()
													,requestListParam.getSortType());
		return new ResponseParam<PageModel<UserCollect>>("数据获取成功", 200, pageByConditions);
	}
	/**
	 * 根据id获取数据
	 * @param id
	 * @return
	 */
	@GetMapping(value="get/{id}")
	public ResponseParam<UserCollect> get(@PathVariable(value="id") Long id) {
		UserCollect userCollect = collectService.get(id);
		return new ResponseParam<UserCollect>("数据获取成功", 200, userCollect);
	}
	/**
	 * 保存数据
	 * @param UserCollect
	 * @return
	 */
	@PostMapping(value="save")
	public ResponseParam<UserCollect> save(@RequestBody UserCollect userCollect) {
		userCollect.setCreateDate(new Date());
		UserCollect user = collectService.save(userCollect);
		return new ResponseParam<UserCollect>("数据保存成功", 200, user);
	}
	/**
	 * 更新数据
	 * @param UserCollect
	 * @return
	 */
	@PostMapping(value="update")
	public ResponseParam<UserCollect> update(@RequestBody UserCollect userCollect) {
		if (userCollect == null || userCollect.getId() != 0L) {
			return new ResponseParam<>("待更新数据没有主键", 500);
		}
		UserCollect user = collectService.save(userCollect);
		return new ResponseParam<UserCollect>("数据更新成功", 200, user);
	}
	/**
	 * 删除数据
	 * @param id
	 * @return
	 */
	@GetMapping(value="delete")
	public ResponseParam<String> delete(@RequestParam("ids") String ids) {
		try {
			String[] split = ids.split(",");
			for (String idStr : split) {
				collectService.deleteById(Long.parseLong(idStr));
			}
		} catch (Exception e) {
			return new ResponseParam<String>(e.getMessage(), 500);
		}
		return new ResponseParam<String>("数据删除成功", 200);
	}
}
